package com.zhxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhxy.pojo.Student;
import com.zhxy.service.StudentService;
import com.zhxy.utils.PageResult;
import com.zhxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/student")
@Api(tags = "学生模块")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/list/{pageNum}/{pageSize}")
    @ApiOperation(value = "学生列表")
    public Result list(@RequestBody Student student,@PathVariable Long pageNum,@PathVariable Long pageSize) throws Exception{
        Page<Student> page = studentService.selectByPage(pageNum, pageSize, student);
        //获取分页数据
        List<Student> records = page.getRecords();
        long total = page.getTotal();
        PageResult<Student> pageResult=new PageResult<>(total,records);
        return Result.ok(pageResult);

    }



    @PostMapping()
    @ApiOperation(value = "增加学生")
    public Result addStudent(@RequestBody Student student) throws Exception{
        int row = studentService.addStudent(student);
        if(row!=1)
            throw new Exception("增加学生失败");
        return Result.ok("增加学生成功");

    }


    @DeleteMapping
    @ApiOperation(value = "批量删除学生")
    public Result deleteStudents(Integer[] ids) throws Exception{
        //数组转集合
        boolean flag = studentService.removeByIds(Arrays.asList(ids));
        if(!flag)
            throw new Exception("批量删除学生失败");
        return Result.ok("批量删除成功");

    }

    @PutMapping
    @ApiOperation(value = "修改学生")
    public Result updateStudent(@RequestBody Student student) throws Exception{

       int row = studentService.update(student);
        if(row!=1)
            throw new Exception("修改学生失败");
        return Result.ok("修改成功");

    }










}
